import rendering,pygame
from vector import vector
class new_window:
    def __init__(self,width,height,title,screen,pos):
        self.title = rendering.text(screen,title,(255,255,255))
        self.button = new_button("x",screen,self.hi)
        self.titleBase = rendering.r_object(screen,(238,44,44),width,20)
        self.width = width
        self.height = height
        self.pos = pos
        self.body = rendering.s_object(screen,(255,255,255),width,height)
        self.body.alpha(100)
        self.objects = []
        self.pressed = False
    def stick(self,o):
        self.objects.append(o)
    def think(self,mouse):
        if rendering.getCollision(mouse,self.titleBase):
            print "Colided"
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    calc = mouse.getVector()
                    calc.sub(mouse.getVector())
                    self.pos.sub(calc)
        i = 0
        self.titleBase.draw(vector(self.pos.get()[0],self.pos.get()[1]-20))
        self.button.think(mouse)
        self.button.draw(vector(self.pos.get()[0]+self.width-self.button.getWidth(),self.pos.get()[1]-20))
        self.body.draw(self.pos)
        self.title.draw(vector(self.pos.get()[0]+self.width/2-self.title.getWidth()/2,self.pos.get()[1]-20))
        for o in self.objects:
            i = i+1
            if i == 1:
                o.draw(vector(self.pos.get()[0]+2,self.pos.get()[1]+2))
                last = o
            else:
                o.draw(vector(self.pos.get()[0]+2,last.endPos()+2))
                last = o
    def color(self,color):
        self.body.color(color)
    def getPos(self):
        return self.pos
    def endPos(self):
        return self.pos[1]+self.height
    def alpha(self,val):
        self.body.alpha(val)
    def hi(self):
        print "END"
class new_button:
    def __init__(self,text,screen,function):
        self.screen = screen
        self.function = function
        self.lenght = len(text)*12
        self.body = rendering.r_object(screen,(0,0,255),self.lenght,20)
        self.text = rendering.text(screen,text,(255,255,255))
        self.hasImage = False
    def stickTo(self,root):
        root.stick(self)
    def draw(self,vec):
        if self.hasImage:
            self.image.draw(vec)
            self.body.draw(vec)
            self.pos = vec
        else:
            self.body.draw(vec)
            self.pos = vec
            self.text.draw(vector(vec.get()[0],vec.get()[1]))
    def think(self,mouse):
        if self.body.returnRect().colliderect(mouse.returnRect()):
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    self.function()
    def getWidth(self):
        return self.text.getWidth()
    def endPos(self):
        if self.hasImage:
            return self.pos.get()[1]+self.height
        else:
            return self.pos.get()[1]+20
    def image(self,path,width,height):
        self.height = height
        self.path = path
        self.image = rendering.i_object(path,self.screen)
        self.image.scale(width,height)
        self.hasImage = True
        self.body = rendering.s_object(self.screen,(255,255,255),width,height)
        self.body.alpha(0)
class new_label:
    def __init__(self,text,screen):
        self.body = rendering.text(screen,text,(255,255,255))
        self.pos = vector(0,0)
    def draw(self,vec):
        self.pos = vec
        self.body.draw(vec)
class new_slider:
    def __init__(self,text,screen,maximum):
        self.description = rendering.text(screen,text,(255,255,255))
        self.color = (255,0,0)
        self.screen = screen
        self.maximum = maximum
        self.body = rendering.line(self.screen,(255,0,0),vector(10,10),vector(10+maximum,10))
        self.mover = rendering.r_object(self.screen,(255,255,255),5,20)
        self.moverVec = vector(0,0)
        self.per = 0
    def stickTo(self,root):
        root.stick(self)
    def draw(self,vec):
        self.body.draw(vector(vec.get()[0],vec.get()[1]+10),vector(vec.get()[0]+self.maximum,vec.get()[1]+10))
        self.moverVec = vector(vec.get()[0]+self.per,vec.get()[1])
        self.mover.draw(self.moverVec)
        self.description.draw(vector(vec.get()[0]+self.maximum,vec.get()[1]))
    def think(self,mouse):
        if self.body.returnRect().colliderect(mouse.returnRect()):
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    vec = vector(pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1])
                    vec.sub(self.mover.getVector())
                    print vec.get()
                    self.per = self.per+vec.get()[0]
    def get(self):
        return self.per
    def endPos(self):
        return self.moverVec.get()[1]+20
class gui_handler:
    def __init__(self,screen):
        pygame.font.init()
        self.screen = screen
        self.windowList = []
        self.buttonList = []
        self.sliderList = []
        self.mouse = rendering.r_object(self.screen,(0,255,0,0),1,1)
        self.isPressed = False
    def update(self):
        self.mousePos = vector(pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1])
        for win in self.windowList:
            win.think(self.mouse)
        for button in self.buttonList:
            button.think(self.mouse)
        for slider in self.sliderList:
            slider.think(self.mouse)
        self.mouse.draw(self.mousePos)
    def addSlider(self,text,size,root):
        s = new_slider(text,self.screen,size)
        root.stick(s)
        self.sliderList.append(s)
        return s
    def addWindow(self,width,height,title,pos):
        w = new_window(width,height,title,self.screen,pos)
        self.windowList.append(w)
        return w
    def addButton(self,text,root,function):
        self.button = new_button(text,self.screen,function)
        self.buttonList.append(self.button)
        root.stick(self.button)
        return self.button
    def addLabel(self,text,root):
        l = new_label(text,self.screen)
        return l
